package com.xiaozhengkeji.servlet.api.user;

import com.google.gson.JsonObject;
import com.xiaozhengkeji.servlet.dao.user.UserReg;
import com.xiaozhengkeji.servlet.servlet.user.UserRegClass;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@WebServlet("/api/user/UserRegApi")
public class UserRegApi extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        Map<String, String[]> dataMap = request.getParameterMap();
        UserReg ur = new UserReg();
        UserRegClass urc = ur.regUser(dataMap.get("username")[0], dataMap.get("password")[0], dataMap.get("email")[0]);
        response.setHeader("Content-type", "application/json; charset=utf-8");
        //要创建JSON格式的数据，首先要创建一个整体的JSON的对象，作为一个容器
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("code", urc.code);
        jsonObject.addProperty("message", urc.message);
        out.write(jsonObject.toString());
    }
}
